package com.personal.sfe.common;


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;

import java.io.Serializable;


@Slf4j
@Data
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> implements Serializable {

    //是否成功
    private boolean success;

    //返回码
    private int code;

    //返回信息
    private String message;

    //返回数据
    private T data;

    public Result(boolean success, int code, String message) {
        this.success = success;
        this.code = code;
        this.message = message;
    }

    public static <T> Result<T> ok() {
        return ok(null);
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>(true, HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), data);
        log.info("接口成功返回:{}", result);
        return result;
    }

    public static <T> Result<T> ok(String msg, T data) {
        Result<T> result = new Result<>(true, HttpStatus.OK.value(), msg, data);
        log.info("接口成功返回:{}", result);
        return result;
    }


    public static <T> Result<T> err(String msg) {
        Result<T> result = new Result<>(true, HttpStatus.INTERNAL_SERVER_ERROR.value(), msg, null);
        log.info("接口成功返回:{}", result);
        return result;
    }

    public static <T> Result<T> err(Integer code, String msg) {
        Result<T> result = new Result<>(false, code, msg, null);
        log.error("接口失败返回:{}", result);
        return result;
    }

    public static <T> Result<T> buildFail400(String message) {
        return Result.err(HttpStatus.BAD_REQUEST.value(), HttpStatus.BAD_REQUEST.getReasonPhrase() + ":" + message);
    }

    public static <T> Result<T> buildFail401(String message) {
        return Result.err(HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase() + ":" + message);
    }

    public static <T> Result<T> buildFail403(String message) {
        return Result.err(HttpStatus.FORBIDDEN.value(), HttpStatus.FORBIDDEN.getReasonPhrase() + ":" + message);
    }

    public static <T> Result<T> buildFail500(String message) {
        return Result.err(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase() + ":" + message);
    }

    public static <T> Result<T> buildFail600(String message) {
        return Result.err(600, message);
    }


    public static <T> void print(int status, HttpServletResponse response, Result<T> result) {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(status);
        try {
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(response.getOutputStream(), result);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public static void print200(HttpServletResponse response, String message) {
        print(HttpStatus.OK.value(), response, ok(message));
    }

    public static void print401(HttpServletResponse response, String message) {
        print(HttpStatus.UNAUTHORIZED.value(), response, buildFail401(message));
    }

    public static void print403(HttpServletResponse response, String message) {
        print(HttpStatus.FORBIDDEN.value(), response, buildFail403(message));
    }
}
